#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <cstdio>

using namespace std;

void* start_routine(void* args)
{ 
    int cnt = 10;
    while(cnt--)
    {
        char buffer[1024];
        //pthread_detach(pthread_self());
        snprintf(buffer, sizeof buffer, "thread %lld", (long long)args);
        cout << buffer << "running ... id: " << pthread_self() << endl;
        sleep(1);
    }
}
int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, start_routine, (void*)1);
    pthread_detach(tid);
    // while(true)
    // {
        cout << "main thread is running, main theadid: " << pthread_self() << "  new thread:" << tid << endl;
        sleep(1); // 先让主线程进行休眠，避免子线程还没有自己分离  缺陷：不知道何时子线程进行分离，所以我们最好让主线程进行分离行为
    // }
    int ret = pthread_join(tid, nullptr);
    cout << "join ret: " << ret << endl;
    return 0;
}